How to Remove Document Management Hurdles with X-Docs?
Ā
Make libraries work for you with JavaScript frameworks
1. How to
make
libraries
work for you
Simon Willison - http://simonwillison.net/
Web 2.0 Expo Berlin
7th November 2007
2. This talk
ā¢ JavaScript libraries!
ā¢ What they are
ā¢ Why they exist
ā¢ What they can do for you
ā¢ How to pick one
3. JavaScript libraries
ā¢ ajaxpatterns.org lists over 40 general purpose
JavaScript libraries
ā¢ ... and thatās not including the many libraries tied
to a speciļ¬c server-side language
ā¢ Why are there so many of them?
4. āThe bad news:
JavaScript is broken.
The good news:
It can be ļ¬xed with
more JavaScript!ā
Geek folk saying
5. ā¢ Inconsistent event model (thanks, IE)
ā¢ Memory management (thanks, IE)
ā¢ The DOM is a horrible API!
ā¢ JavaScript-the-language has quite a few warts
ā¢ But itās powerful enough to let you ļ¬x them
ā¢ Browser Ajax is far too verbose
ā¢ Positioning and co-ordinates
ā¢ Drag and drop and Animation are really hard
6. var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {}
}
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
alert(xhr.responseText);
} else {
alert('Error code ' + xhr.status);
}
}
}
7. Narrowing them down...
ā¢ Prototype (and Scriptaculous)
ā¢ The Yahoo! User Interface Library - YUI
ā¢ jQuery
ā¢ The Dojo Toolkit
8. Download
Get the latest versionā1.5.1
Learn
Prototype is a JavaScript Framework that aims to Online documentation and resources.
ease development of dynamic web applications.
Featuring a unique, easy-to-use toolkit for class-driven Discuss
development and the nicest Ajax library around, Prototype Mailing list and IRC
is quickly becoming the codebase of choice for web
application developers everywhere.
Contribute
Submit patches and report bugs.
Prototype and Scriptaculous
Prototype and script.aculo.us: The quot;Bungee
bookquot; has landed!
Core team member Christophe
Who's using Prototype?
Meet the developers
Porteneuve has been hard at work
for the past few months tracking
9. ā¢ Prototype focuses on basic browser
compatibility and JavaScript language
enhancement
ā¢ It tries to make JavaScript more like Ruby
ā¢ Extends most of JavaScriptās built-in objects
with new functionality
ā¢ Scriptaculous adds fancy effects, basic
widgets and drag and drop
14. ā¢ Created at Yahoo!, BSD licensed
ā¢ Designed for both creating new applications
and integration with legacy code
ā¢ Focused on browser issues; almost no
functionality relating to JS language itself
ā¢ Extensively tested and documented
15. controls
autocomplete calendar container
menu slider treeview
animation dragdrop
dom event connection
utilities
16. YAHOO.util.Event.on(window, 'load', function() {
var div = YAHOO.util.Dom.get('messages');
setTimeout(function() {
var anim = new YAHOO.util.Anim(div, {
height: {to: 0},
opacity: {to: 0}
}, 0.4);
anim.animate();
anim.onComplete.subscribe(function() {
div.parentNode.removeChild(div);
});
}, 2000);
});
17. Common YUI idiom
$E = YAHOO.util.Event;
$D = YAHOO.util.Dom;
$E.on(window, 'load', function() {
var div = $D.get('messages');
...
});
19. ā¢ Simple philosophy: ļ¬nd some nodes, then do
something to them
ā¢ Minimal impact on your global namespace - it adds
two global symbols: jQuery and $, and $ can be easily
reverted
ā¢ API designed around āchainingā - other libraries are
now emulating this
ā¢ Outstanding node selection, based on CSS 3 and
custom extensions
ā¢ Small core library with a smart plugin mechanism
23. ā¢ The oldest of the current popular libraries,
pre-dating even the term āAjaxā
ā¢ Incredible amounts of functionality
ā¢ Used to suffer from a tough learning curve,
but the 1.0 release greatly simpliļ¬es things
31. ā¢ Graphics (cross-browser drawing API)
ā¢ Ofļ¬ine storage
ā¢ Cryptography
ā¢ Templating
ā¢ Data grids and more
ā¢ āThe future of the browser todayā
37. Smart node selection
ā¢ Any JavaScript that modiļ¬es the page inevitably
starts out by selecting some existing nodes
ā¢ jQuery is based entirely around node selection
ā¢ Prototype and Dojo also have node selection APIs
38. Smarter Ajax
ā¢ Prototype makes it easy to set a callback for
when ANY Ajax request completes... useful
for loading status icons
ā¢ Ajax.Updater can extract and execute
<script> blocks in HTML fragments
ā¢ Great for unobtrusively enhancing elements
that have just been added to the page
39. Self-adjusting animations
ā¢ You can roll your own animations in
JavaScript using setTimeout and setInterval...
ā¢ ... but the time taken for a animation will
vary depending on browser performance
ā¢ Smarter animations adjust their framerate to
compensate for browser performance
ā¢ All four libraries do this
40. DSLs for animation
var anim = new YAHOO.util.Anim('el', {
opacity: {to: 0.5},
height: {to: 0},
fontSize: {
from: 100, to: 50, unit: '%'
}
}, 1);
anim.animate();
41. XPath optimisations
ā¢ Mozilla and Opera offer fast XPath lookups
through document.evaluate(...)
ā¢ Dojo can use this for getElementsByClass()
ā¢ Prototype redeļ¬nes getElementsBySelector
to use XPath
42. Miniļ¬cation
ā¢ All four libraries ship in both uncompressed
and compressed formats
ā¢ YUI uses miniļ¬cation: whitespace and
comments are stripped
ā¢ The Dojo build system uses āShrinkSafeā,
which compresses JavaScript using the Rhino
parser
ā¢ jQuery uses Dean Edwardsā Packer, with
base62 encoding
43. Hosting on a CDN
http://yui.yahooapis.com/2.2.2/build/reset/reset-min.css
http://yui.yahooapis.com/2.2.2/build/dom/dom-min.js
...
http://o.aolcdn.com/iamalpha/.resource/jssdk/dojo-0.4.1/dojo.js
ā¢ JavaScript is cached before the user even visits your site
45. So how do you pick one?
ā¢ It depends on what you are trying to build
46.
47. My library selection criteria
ā¢ Enables unobtrusive JavaScript
ā¢ Plays well with other code
ā¢ Smart use of namespacing
ā¢ Global variable impact kept to a minimum
ā¢ Tie breaker: the less code I have to write the
better!
48. ā¢ Iām currently using and recommending
jQuery for most situations
ā¢ But... thereās cut-throat competition
between the various libraries at the moment
ā¢ This is one of the reasons I care about
interoperability - commit to a single library
and you might lose out when one of the
others jumps ahead of it